package com.zzh.partnersys.im.handler;

import cn.hutool.extra.spring.SpringUtil;
import com.google.gson.Gson;
import com.zzh.partnersys.im.message.request.ChatRequestMessage;
import com.zzh.partnersys.im.rocketmq.contant.MQConstant;
import com.zzh.partnersys.im.rocketmq.producer.MessageSender;
import com.zzh.partnersys.im.sensitiveword.algorithm.SensitiveWordBs;
import com.zzh.partnersys.im.session.SessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

/**
 * @author ??????
 * @version 1.0
 */
//ר��ֻ����ChatRequestMessage���͵���Ϣ��Handler,������Ϣ��ĳ���û�
@Sharable
public class ChatRequestMessageHandler extends SimpleChannelInboundHandler<ChatRequestMessage> {
    Gson gson = new Gson();

    private SensitiveWordBs sensitiveWordBs;

    private MessageSender messageSender;

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ChatRequestMessage chatRequestMessage) throws Exception {
        System.out.println("chatRequestMessage"+ chatRequestMessage);
        if (chatRequestMessage == null){

        }
        Long toUid = chatRequestMessage.getUid();//���͸��ĸ��û�
        String content = chatRequestMessage.getContent();//��Ϣ����
        //�������ʻ��??
        if (sensitiveWordBs == null){
            sensitiveWordBs = getSensitiveWordBs();
        }
        chatRequestMessage.setContent(sensitiveWordBs.filter(content));



        //�鿴�û��Ƿ��¼??todo û��¼�����Ƚ��л�������/redis??
        Channel channel = SessionFactory.getSession().getChannel(toUid);
        if (channel != null){
            System.out.println("����");
            //Ҫ��TextWebSocketFrame֡����ʽд��??WebSocket)
            String json = gson.toJson(chatRequestMessage);
            channel.writeAndFlush(new TextWebSocketFrame(json));
        }

        //rocketMQ������??
        if (messageSender == null){
            messageSender = getMessageSender();
        }
        messageSender.sendMsg(MQConstant.CHAT_ONE_TOPIC,chatRequestMessage);



        //�洢�����ݿ�??
//        if (friendMessageService == null){
//            friendMessageService = getFriendMessageService();
//        }
//        //������Ϣ��������
//        FriendMessageDO friendMessage = new FriendMessageDO();
//        friendMessage.setFriendKey(FriendKeyUtils.getFriendKeyUID(toUid,fromUid));
//        friendMessage.setFromUid(fromUid);
//        friendMessage.setContent(content);
//
//        Long id = friendMessageService.insertData(friendMessage);
//        if (id != null){
//            //��Ϣ�洢�ɹ�
//
//        }else {
//            //todo ��Ϣ�洢ʧ�ܣ���·���⣿���ݿ���??
//
//        }
    }

    protected MessageSender getMessageSender(){
        return SpringUtil.getBean(MessageSender.class);
    }



    //��ȡBean����
    protected SensitiveWordBs getSensitiveWordBs(){
        return SpringUtil.getBean(SensitiveWordBs.class);
    }


//    private FriendMessageService getFriendMessageService(){
//        return SpringUtil.getBean(FriendMessageService.class);
//    }
}
